/*
* $Id: SettingsRecord.java 265 2011-02-07 23:13:07Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.settings;

import java.util.Date;

/**
 * Data of settings parameter
 * @author lexus
 */
public final class SettingsRecord
{
//--------------------------Names of parameters---------------------------------
    public static final int NOTE_POINT   = 1;

    public static final int TYPE_FAMILY  = 3;
    public static final int NOTE_FAMILY  = 4;

    public static final int GOODS_HONEY  = 6;
    public static final int PUMP_HONEY   = 7;
    public static final int FEEDING      = 8;
    public static final int OPEN_BROOD   = 9;
    public static final int SEAL_BROOD   = 10;
    public static final int GIVE_WOX     = 11;
    public static final int AMBROSIA     = 12;
    public static final int DISEASES     = 13;
    public static final int NOTE_INSPECT = 14;

//--------------------------Types of parameters---------------------------------
    public static final int TYPE_VALUE_INT = 1;
    public static final int TYPE_VALUE_STRING = 2;
    public static final int TYPE_VALUE_BOOL = 3;
    public static final int TYPE_VALUE_DOUBLE = 4;
    public static final int TYPE_VALUE_DATE = 5;

    /**
     * Container of parameter data
     * @author lexus
     */
    public class Field{
        /** Identificator of parameter */
        private Integer idRecord;
        /** Value parameter */
        private Object value;
        /** Type parameter */
        private Integer typeValue;

        /**
         * Constructor
         */
        public Field(){
            idRecord = null;
            typeValue= null;
            value = null;
        }
        /**
         * Constructor
         * @param id identificator of parameter
         * @param type type parameter
         * @param val value parameter
         */
        public Field(Integer id, Integer type, String val){
            idRecord = id;
            typeValue= type;
            setVal(val);
        }

        /**
         * Sets value parameter
         * @param val value parameter.
         * Value wiil be represented in the type of.
         */
        public void setValue(Object val) {
            if(val.getClass().getName().equals(Boolean.class.getName())) {
                typeValue = new Integer(TYPE_VALUE_BOOL);
            } else if (val.getClass().getName().equals(Integer.class.getName())) {
                typeValue = new Integer(TYPE_VALUE_INT);
            } else if(val.getClass().getName().equals(String.class.getName())){
                typeValue = new Integer(TYPE_VALUE_STRING);
            } else if(val.getClass().getName().equals(Date.class.getName())){
                typeValue = new Integer(TYPE_VALUE_DATE);
            } else if(val.getClass().getName().equals(Double.class.getName())){
                typeValue = new Integer(TYPE_VALUE_DOUBLE);
            }
            value = val;
        }
        /**
         * Gets value parameter
         * @return value parameter.
         * Value is represented in the type of.
         */
        public Object getValue() {
            return value;
        }
        /**
         * Gets type parameter
         * @return type parameter
         */
        public Integer getTypeValue() {
            return typeValue;
        }
        /**
         * Gets identificator parameter
         * @return identificator parameter
         */
        public Integer getIdRecord() {
            return idRecord;
        }

        /**
         * Sets value parameter
         * @param val value parameter.
         * Value will be converted in the type of
         */
        private void setVal(String val){
            if(typeValue.intValue() == TYPE_VALUE_BOOL){
                value = (val.equals(Boolean.TRUE.toString()) ? Boolean.TRUE : Boolean.FALSE);
            }else if (typeValue.intValue() == TYPE_VALUE_INT){
                value = new Integer(Integer.parseInt(val));
            } else if (typeValue.intValue() == TYPE_VALUE_STRING){
                value = val;
            } else if (typeValue.intValue() == TYPE_VALUE_DATE){
                value = new Date(Long.parseLong(val));
            } else if (typeValue.intValue() == TYPE_VALUE_DOUBLE){
                value = new Double(Double.parseDouble(val));
            }
        }

        /**
         * Validation parameter
         * @return parameter is valid or not
         */
        public boolean isValid(){
            return typeValue != null && value != null;
        }

        /**
         * Checks conservation parameter
         * @return parameter is saved or not
         */
        public boolean isRecord(){
            return idRecord != null;
        }

        /**
         * Sets identification of parameter.
         * Call when parameter was saved in the storage.
         * @param addRecord identification of parameter
         */
       protected void setIdRecord(int addRecord) {
            idRecord = new Integer(addRecord);
        }

    }

    /** Name parameter */
    public Integer name;
    /** Value parameter */
    public Field field;
    
    /**
     * Contructor
     * @param nameRecord name parameter
     */
    public SettingsRecord(Integer nameRecord){
        name = nameRecord;
        field = new Field();
    }

    /**
     * Sets data of parameter
     * @param data data of parameter
     */
    public void setField(Field data) {
        field = data;
    }

}
